//
//  WJCityPikerView.m
//  yygroupon
//
//  Created by apple on 15/6/3.
//  Copyright (c) 2015年 waste. All rights reserved.
//

#import "WJCityPikerView.h"
#import "DatabaseSql.h"
@interface WJCityPikerView () {
    NSInteger _width;
    NSInteger _height;
    UIPickerView *_piker;
    NSInteger _index1;
    NSString *string1;
}

@property (nonatomic,strong)NSArray *array;

@end

@implementation WJCityPikerView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.frame = CGRectMake(0,ScreenHeight-220-64, [UIScreen mainScreen].bounds.size.width, 220);
        _width = self.frame.size.width;
        _height = self.frame.size.height;
        _model = [[WJCityPikerModel alloc]init];
//        self.backgroundColor = [UIColor orangeColor];
        [self initView];
    }
    return self;
}

- (instancetype)initWithProvinces:(NSArray *)provinces city:(NSArray *)city distrct:(NSArray *)distrct{
    self = [self init];
    if (self) {
        _provinces = [[NSArray alloc]initWithArray:provinces];
        _city = [[NSArray alloc]initWithArray:city];
        _distrct = [[NSArray alloc]initWithArray:distrct];
        _model.provinceId = @"1";
        _model.provinceName = @"北京市";
        _model.cityId = @"100001";
        _model.cityName = @"东城区";
    }
    return self;
}

#pragma mark - 懒加载

- (NSArray *)array {
    if (!_array) {
        _array = [[NSArray alloc]initWithObjects:@"完成", nil];
    }
    return _array;
}

#pragma mark - initView

- (void)initView {
    _piker = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, _width, _height-40)];
    _piker.dataSource = self;
    _piker.delegate =self;
    [self addSubview:_piker];

    for (int i = 0; i < self.array.count; i ++) {
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(_width/self.array.count *i, _height-40, _width/self.array.count , 40)];
        [button setTitle:self.array[i] forState:UIControlStateNormal];
        button.tag = 200 + i;
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitleColor:[UIColor colorWithHexString:@"33ccbf"] forState:UIControlStateNormal];
        [self addSubview:button];
    }
    
    UIView *line0 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _width, 1)];
    line0.backgroundColor = [UIColor lightGrayColor];
    line0.alpha = 0.3;
    [self addSubview:line0];
    
    UIView *line1 = [[UIView alloc]initWithFrame:CGRectMake(0, _height-40, _width, 1)];
    line1.backgroundColor = [UIColor lightGrayColor];
    line1.alpha = 0.3;
    [self addSubview:line1];
    UIView *line2 = [[UIView alloc]initWithFrame:CGRectMake(_width/2, _height-40, 1, 40)];
    line2.backgroundColor = [UIColor lightGrayColor];
    line2.alpha = 0.3;
//    [self addSubview:line2];
    
}

#pragma mark - 事件监听

- (void)buttonPressed:(UIButton *)sender {

    NSInteger index= sender.tag - 200;
    if (index == 0) {
        if (_modelBlock) {
            _modelBlock(_model);
        }
    }else {
        if (_backBlock) {
            _backBlock();
        }
    }
}

#pragma mark -<UIPickerViewDataSource>

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    switch (component) {
        case 0:
            //_provinces.count共有87个省 前有34🇨🇳省份
            if (_isContains) {
                
                return _provinces.count;
            }
            else
            {
                 return 34;
            }
            break;
        case 1:
            return _city.count;
            break;
        case 2:
            return _distrct.count;
            break;
        default:
            return 0;
            break;
    }
}

#pragma mark - <UIPickerViewDelegate>

//配置标题
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    switch (component) {
        case 0:
            return _provinces[row][@"proname"];
            break;
        case 1:
            return _city[row][@"name"];
            
            break;
        case 2:
            return _distrct[row][@"name"];
            break;
        default:
            return 0;
            break;
    }
}

//点击了
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    switch (component) {
        case 0:
            //省份
          
            _index1 = row;
            _city = [[NSArray alloc]initWithArray:[DatabaseSql queryCityForProvinces:[NSString stringWithFormat:@"%ld",_index1+1]]];
            if (_city.count >0) {
                string1 = _city[0][@"id"];
                _distrct = [[NSArray alloc]initWithArray:[DatabaseSql findprovid:[NSString stringWithFormat:@"%ld",_index1+1] cityID:string1]];
                
            }else {
                _distrct = [[NSArray alloc]init];
                _city = [[NSArray alloc]initWithArray:[DatabaseSql findprovid:[NSString stringWithFormat:@"%ld",_index1+1] cityID:@"0"]];
            }
            
            [_piker selectRow:0 inComponent:1 animated:NO];
        
//                        [_piker reloadAllComponents];
            [_piker reloadComponent:1];
            [_piker reloadComponent:2];
            
            string1 = _city[0][@"id"];
            _model.provinceId = [NSString stringWithFormat:@"%ld",_index1 + 1];
            _model.provinceName = _provinces[row][@"proname"];
            if (_city.count > 0) {
                _model.cityName = _city[0][@"name"];
            }else {
                _model.cityId = nil;
                _model.cityName = nil;
            }
            _model.cityId = string1;
            if (_distrct.count > 0) {
                _model.disId = _distrct[0][@"id"];
                _model.disName = _distrct[0][@"name"];
                
            }else {
                _model.disId = nil;
                _model.disName = nil;
            }
      
            break;
        case 1:
            string1 = _city[row][@"id"];
            _distrct = [[NSArray alloc]initWithArray:[DatabaseSql findprovid:[NSString stringWithFormat:@"%ld",_index1 +1] cityID:string1]];
            
            _model.cityId = string1;
            _model.cityName = _city[row][@"name"];
            
            if (_distrct.count > 0) {
                _model.disId = _distrct[0][@"id"];
                _model.disName = _distrct[0][@"name"];
                
            }else {
                _model.disId = nil;
                _model.disName = nil;
            }
            
             [_piker selectRow:0 inComponent:2 animated:NO];
             [_piker reloadComponent:2];
            break;
        case 2:
            if (_distrct.count >0) {
                _model.disId = _distrct[row][@"id"];
                _model.disName = _distrct[row][@"name"];
            }else {
                _model.disId = nil;
                _model.disName = nil;
            }
            break;
        default:
            break;
    }
}

@end
